home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlipc.1 < prev    next >
Text File  |  1995-07-25  |  8KB  |  265 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLIIIIPPPPCCCC((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLIIIIPPPPCCCC((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlipc - Perl interprocess communication
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.           The IPC facilities of Perl are built on the Berkeley socket
  13.           mechanism.  If you don't have sockets, you can ignore this
  14.           section.  The calls have the same names as the corresponding
  15.           system calls, but the arguments tend to differ, for two
  16.           reasons.  First, Perl file handles work differently than C
  17.           file descriptors.  Second, Perl already knows the length of
  18.           its strings, so you don't need to pass that information.
  19.  
  20.           CCCClllliiiieeeennnntttt////SSSSeeeerrrrvvvveeeerrrr CCCCoooommmmmmmmuuuunnnniiiiccccaaaattttiiiioooonnnn
  21.  
  22.           Here's a sample TCP client.
  23.  
  24.               ($them,$port) = @ARGV;
  25.               $port = 2345 unless $port;
  26.               $them = 'localhost' unless $them;
  27.  
  28.               $SIG{'INT'} = 'dokill';
  29.               sub dokill { kill 9,$child if $child; }
  30.  
  31.               use Socket;
  32.  
  33.               $sockaddr = 'S n a4 x8';
  34.               chop($hostname = `hostname`);
  35.  
  36.               ($name, $aliases, $proto) = getprotobyname('tcp');
  37.               ($name, $aliases, $port) = getservbyname($port, 'tcp')
  38.                   unless $port =~ /^\d+$/;
  39.               ($name, $aliases, $type, $len, $thisaddr) =
  40.                               gethostbyname($hostname);
  41.               ($name, $aliases, $type, $len, $thataddr) = gethostbyname($them);
  42.  
  43.               $this = pack($sockaddr, &AF_INET, 0, $thisaddr);
  44.               $that = pack($sockaddr, &AF_INET, $port, $thataddr);
  45.  
  46.               socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
  47.               bind(S, $this) || die "bind: $!";
  48.               connect(S, $that) || die "connect: $!";
  49.  
  50.               select(S); $| = 1; select(stdout);
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLIIIIPPPPCCCC((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLIIIIPPPPCCCC((((1111))))
  71.  
  72.  
  73.  
  74.               if ($child = fork) {
  75.                   while (<>) {
  76.                       print S;
  77.                   }
  78.                   sleep 3;
  79.                   do dokill();
  80.               }
  81.               else {
  82.                   while (<S>) {
  83.                       print;
  84.                   }
  85.               }
  86.  
  87.           And here's a server:
  88.  
  89.               ($port) = @ARGV;
  90.               $port = 2345 unless $port;
  91.  
  92.               use Socket;
  93.  
  94.               $sockaddr = 'S n a4 x8';
  95.  
  96.               ($name, $aliases, $proto) = getprotobyname('tcp');
  97.               ($name, $aliases, $port) = getservbyname($port, 'tcp')
  98.                   unless $port =~ /^\d+$/;
  99.  
  100.               $this = pack($sockaddr, &AF_INET, $port, "\0\0\0\0");
  101.  
  102.               select(NS); $| = 1; select(stdout);
  103.  
  104.               socket(S, &PF_INET, &SOCK_STREAM, $proto) || die "socket: $!";
  105.               bind(S, $this) || die "bind: $!";
  106.               listen(S, 5) || die "connect: $!";
  107.  
  108.               select(S); $| = 1; select(stdout);
  109.  
  110.               for (;;) {
  111.                   print "Listening again\n";
  112.                   ($addr = accept(NS,S)) || die $!;
  113.                   print "accept ok\n";
  114.  
  115.                   ($af,$port,$inetaddr) = unpack($sockaddr,$addr);
  116.                   @inetaddr = unpack('C4',$inetaddr);
  117.                   print "$af $port @inetaddr\n";
  118.  
  119.                   while (<NS>) {
  120.                       print;
  121.                       print NS;
  122.                   }
  123.               }
  124.  
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLIIIIPPPPCCCC((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLIIIIPPPPCCCC((((1111))))
  137.  
  138.  
  139.  
  140.           SSSSyyyyssssVVVV IIIIPPPPCCCC
  141.  
  142.           Here's a small example showing shared memory usage:
  143.  
  144.               $IPC_PRIVATE = 0;
  145.               $IPC_RMID = 0;
  146.               $size = 2000;
  147.               $key = shmget($IPC_PRIVATE, $size , 0777 );
  148.               die if !defined($key);
  149.  
  150.               $message = "Message #1";
  151.               shmwrite($key, $message, 0, 60 ) || die "$!";
  152.               shmread($key,$buff,0,60) || die "$!";
  153.  
  154.               print $buff,"\n";
  155.  
  156.               print "deleting $key\n";
  157.               shmctl($key ,$IPC_RMID, 0) || die "$!";
  158.  
  159.           Here's an example of a semaphore:
  160.  
  161.               $IPC_KEY = 1234;
  162.               $IPC_RMID = 0;
  163.               $IPC_CREATE = 0001000;
  164.               $key = semget($IPC_KEY, $nsems , 0666 | $IPC_CREATE );
  165.               die if !defined($key);
  166.               print "$key\n";
  167.  
  168.           Put this code in a separate file to be run in more that one
  169.           process Call the file _t_a_k_e:
  170.  
  171.               # create a semaphore
  172.  
  173.               $IPC_KEY = 1234;
  174.               $key = semget($IPC_KEY,  0 , 0 );
  175.               die if !defined($key);
  176.  
  177.               $semnum = 0;
  178.               $semflag = 0;
  179.  
  180.               # 'take' semaphore
  181.               # wait for semaphore to be zero
  182.               $semop = 0;
  183.               $opstring1 = pack("sss", $semnum, $semop, $semflag);
  184.  
  185.               # Increment the semaphore count
  186.               $semop = 1;
  187.               $opstring2 = pack("sss", $semnum, $semop,  $semflag);
  188.               $opstring = $opstring1 . $opstring2;
  189.  
  190.               semop($key,$opstring) || die "$!";
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLIIIIPPPPCCCC((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLIIIIPPPPCCCC((((1111))))
  203.  
  204.  
  205.  
  206.           Put this code in a separate file to be run in more that one
  207.           process Call this file _g_i_v_e:
  208.  
  209.               #'give' the semaphore
  210.               # run this in the original process and you will see
  211.               # that the second process continues
  212.  
  213.               $IPC_KEY = 1234;
  214.               $key = semget($IPC_KEY, 0, 0);
  215.               die if !defined($key);
  216.  
  217.               $semnum = 0;
  218.               $semflag = 0;
  219.  
  220.               # Decrement the semaphore count
  221.               $semop = -1;
  222.               $opstring = pack("sss", $semnum, $semop, $semflag);
  223.  
  224.               semop($key,$opstring) || die "$!";
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.